// JavaScript Document
var observe;
if (window.attachEvent) {
	observe = function(element, event, handler) {
		element.attachEvent('on' + event, handler);
	};
} else {
	observe = function(element, event, handler) {
		element.addEventListener(event, handler, false);
	};
}
function init() {
	var text = document.getElementById('text');
	function resize() {
		text.style.height = 'auto';
		text.style.height = text.scrollHeight + 'px';
	}
	/* 0-timeout to get the already changed text */
	function delayedResize() {
		window.setTimeout(resize, 0);
	}
	observe(text, 'change', resize);
	observe(text, 'cut', delayedResize);
	observe(text, 'paste', delayedResize);
	observe(text, 'drop', delayedResize);
	observe(text, 'keydown', delayedResize);
	text.focus();
	text.select();
	resize();
}

// 控件编辑模式
function editinput() {
	$("#edit").attr("value", "完成");
	$("#name").removeAttr("readOnly");
	$("#age").removeAttr("readOnly");
	$("#sex").removeAttr("disabled");
	$("#zy").removeAttr("readOnly");
	$("#text").removeAttr("disabled");
	$("#wechat").removeAttr("readOnly");
	$("#school").removeAttr("readOnly");
	$("#exchect").removeAttr("readOnly");
}
// 控件固定模式
function saveinput() {
	$("#edit").attr("value", "编辑");
	$("#name").attr("readOnly", "readOnly");
	$("#age").attr("readOnly", "readOnly");
	$("#sex").attr("disabled", "disabled");
	$("#zy").attr("readOnly", "readOnly");
	$("#text").attr("disabled", "disabled");
	$("#wechat").attr("readOnly", "readOnly");
	$("#school").attr("readOnly", "readOnly");
	$("#exchect").attr("readOnly", "readOnly");
}

function editbtn() {
	var edit = $("#edit").val();
	if (edit == "编辑") {
		editinput();

	}
	if (edit == "完成") {
		// 验证每个字段信息正确性
		if (RegName() && RegAge()) {
			$(".userPhone").remove();// 成功后清除警告提示
			changenews();
			saveinput();

		}
	}
}
// 修改信息
function changenews() {
	var strQuery = $("#name").val() + "," + $("#age").val() + ","
			+ $("#sex option:selected").val() +","+ $("#tel").val() + ","
			+ $("#wechat").val() + "," + $("#school").val() + ","
			+ $("#zy").val() + "," + $("#exchect").val() + ","
			+ $("#text").val();
	// window.location.href="changePersonalServlet.html";
	$.ajax({
		url : "changePersonalServlet.scx.szb",
		type : "Post",
		cache : false,
		data : "changedate=" + strQuery,
		dataype : 'text',
		success : function(msg) {
			if (msg == "success") {
				$(".userPhone").remove();
				$("<label   class='successts userPhone'>修改成功!</label>").insertAfter("#pertitle");
			}
			if(msg=="default"){
				$(".userPhone").remove();
				$("<label   class='error userPhone'>修改失败!</label>").insertAfter("#pertitle");	
			}
		}
	});
}

// 验证是否是数字
function RegAge() {
	var reg = new RegExp("^[0-9]*$");
	var age = $("#age").val();
	if (!reg.test(age)) {
		$(".userPhone").remove();
		$("<label   class='error userPhone'>年龄必须为整数!</label>").insertAfter(
				"#pertitle");
		return false;
	}
	return true;
}

// 是否为汉字
function RegName() {
	var reg = /^[\u4E00-\u9FA5]+$/;
	var str = $("#name").val();
	if (!reg.test(str)) {
		$(".userPhone").remove();
		$("<label   class='error userPhone'>姓名输入有误!</label>").insertAfter(
				"#pertitle");
		return false;
	}
	return true;
}